// SettingsForm.cs created with MonoDevelop
// User: markus at 09.33 2007-10-28
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using Gtk;
using Mono.Unix;

namespace WtSharp
{
	
	
	public partial class SettingsForm : Gtk.Bin
	{
		private Gtk.Window m_ParentWindow;
		private WtSettings m_Settings;
		private SpecialCharacters m_SpecialChars;
		private PageSettings m_PageSettings;
		private WtDocument m_document;
		
		public SettingsForm(Gtk.Window parent, WtSettings settings, SpecialCharacters specialChars)
		{
			m_ParentWindow = parent;
			m_Settings = settings;
			m_SpecialChars = specialChars;
			this.Build();
			LoadSettings();
			UpdateSpecialCharactersButtons();
		}
		
		public WtDocument Document 
		{
			set
			{
				m_document = value;
			}
		}
		
		private void LoadSettings()
		{
			entrySettingsPause.Text = ""+0.8;
			entrySettingsName.Text = m_Settings.GetString("Name", "");
			m_SpecialChars.Char1 = m_Settings.GetString("Special1", m_SpecialChars.Char1);
			m_SpecialChars.Char2 = m_Settings.GetString("Special2", m_SpecialChars.Char2);
			m_SpecialChars.Char3 = m_Settings.GetString("Special3", m_SpecialChars.Char3);
			m_SpecialChars.Char4 = m_Settings.GetString("Special4", m_SpecialChars.Char4);
			radiobuttonSettingsPractiseAll.Active = m_Settings.GetBool("PractiseAll", radiobuttonSettingsPractiseAll.Active);
			entrySettingsPractiseMaxNum.Text = m_Settings.GetString("PractiseMaxNum", entrySettingsPractiseMaxNum.Text);
			checkbuttonSettingsMix.Active = m_Settings.GetBool("MixWords", checkbuttonSettingsMix.Active);
			checkbuttonSettingsCase.Active = m_Settings.GetBool("CaseSensitive", checkbuttonSettingsCase.Active);
			checkbuttonSettingsPartOfSpeech.Active = m_Settings.GetBool("ShowWordType", checkbuttonSettingsPartOfSpeech.Active);
			checkbuttonSettingsInstantCorrection.Active = m_Settings.GetBool("InstantCorrection", checkbuttonSettingsInstantCorrection.Active);
			entrySettingsPause.Text = m_Settings.GetString("PauseTime", entrySettingsPause.Text);
			
			bool PlayAtWrong = m_Settings.GetBool("PlayAtWrong", false);		
			checkbuttonSettingsWrong.Active = PlayAtWrong;
			entrySettingsWrongSound.Sensitive = PlayAtWrong;
			buttonSettingsOpenWrongSound.Sensitive = PlayAtWrong;
			buttonSettingsPlayWrongSound.Sensitive = PlayAtWrong;
			
			bool PlayAtRight = m_Settings.GetBool("PlayAtRight", false);
			checkbuttonSettingsRight.Active = PlayAtRight;
			entrySettingsRightSound.Sensitive = PlayAtRight;
			buttonSettingsOpenRightSound.Sensitive = PlayAtRight;
			buttonSettingsPlayRightSound.Sensitive = PlayAtRight;

			
			entrySettingsRightSound.Text = m_Settings.GetString("RightSound", entrySettingsRightSound.Text);
			entrySettingsWrongSound.Text = m_Settings.GetString("WrongSound", entrySettingsWrongSound.Text);
			
			
			
			// Page Settings
			object pageSettings = m_Settings.GetObject("PageSettings");
			if(pageSettings != null)
				m_PageSettings = (PageSettings)pageSettings;
			else
				m_PageSettings = new PageSettings();
			
		}
		
		public void SaveSettings()
		{
			m_Settings.SetValue("Name", entrySettingsName.Text);
			m_Settings.SetValue("Special1", m_SpecialChars.Char1);
			m_Settings.SetValue("Special2", m_SpecialChars.Char2);
			m_Settings.SetValue("Special3", m_SpecialChars.Char3);
			m_Settings.SetValue("Special4", m_SpecialChars.Char4);
			m_Settings.SetValue("PractiseAll", radiobuttonSettingsPractiseAll.Active);
			m_Settings.SetValue("PractiseMaxNum", entrySettingsPractiseMaxNum.Text);
			m_Settings.SetValue("MixWords", checkbuttonSettingsMix.Active);
			m_Settings.SetValue("CaseSensitive", checkbuttonSettingsCase.Active);
			m_Settings.SetValue("ShowWordType", checkbuttonSettingsPartOfSpeech.Active);
			m_Settings.SetValue("InstantCorrection", checkbuttonSettingsInstantCorrection.Active);
			m_Settings.SetValue("PauseTime", entrySettingsPause.Text);
			m_Settings.SetValue("PlayAtWrong", checkbuttonSettingsWrong.Active);
			m_Settings.SetValue("PlayAtRight", checkbuttonSettingsRight.Active);
			m_Settings.SetValue("WrongSound", entrySettingsWrongSound.Text);
			m_Settings.SetValue("RightSound", entrySettingsRightSound.Text);
			
			m_Settings.SetValue("PageSettings", m_PageSettings);
			
			//m_Settings.Save();
		}

		private void UpdateSpecialCharactersButtons()
		{
			buttonSettingsSpecial1.Label = m_SpecialChars.Label1;
			buttonSettingsSpecial2.Label = m_SpecialChars.Label2;
			buttonSettingsSpecial3.Label = m_SpecialChars.Label3;
			buttonSettingsSpecial4.Label = m_SpecialChars.Label4;
		}
		
		private FileChooserDialog CreateOpenSoundDialog(string currentFilename)
		{
			FileChooserDialog fc = new Gtk.FileChooserDialog(Catalog.GetString("Choose sound file"),
			                                                     m_ParentWindow,
			                                                     FileChooserAction.Open,
			                                                     "Cancel",ResponseType.Cancel,
			                                                     "Open",ResponseType.Accept);

			if(currentFilename.IndexOf(System.IO.Path.DirectorySeparatorChar) != -1)
			{
				try
				{
					currentFilename = currentFilename.Substring(0, currentFilename.LastIndexOf(System.IO.Path.DirectorySeparatorChar));
					fc.SetCurrentFolder(currentFilename);
				}
				catch(Exception) {}
			}
			
			FileFilter filter1 = new FileFilter();
			filter1.AddPattern("*.wav");
			filter1.Name = Catalog.GetString("Wave-files (.wav)");
			fc.AddFilter(filter1);
			
			return fc;
		}

		protected virtual void OnButtonSettingsSpecial1Clicked (object sender, System.EventArgs e)
		{
			CharactersDialog dlg = new CharactersDialog(m_ParentWindow);
			dlg.Character = m_SpecialChars.Char1;
			dlg.Run();
			if(dlg.Character != null)
			{
				m_SpecialChars.Char1 = dlg.Character;
				UpdateSpecialCharactersButtons();
			}
		}

		protected virtual void OnButtonSettingsSpecial2Clicked (object sender, System.EventArgs e)
		{
			CharactersDialog dlg = new CharactersDialog(m_ParentWindow);
			dlg.Character = m_SpecialChars.Char2;
			dlg.Run();
			if(dlg.Character != null)
			{
				m_SpecialChars.Char2 = dlg.Character;
				UpdateSpecialCharactersButtons();
			}
		}

		protected virtual void OnButtonSettingsSpecial3Clicked (object sender, System.EventArgs e)
		{
			CharactersDialog dlg = new CharactersDialog(m_ParentWindow);
			dlg.Character = m_SpecialChars.Char3;
			dlg.Run();
			if(dlg.Character != null)
			{
				m_SpecialChars.Char3 = dlg.Character;
				UpdateSpecialCharactersButtons();
			}
		}

		protected virtual void OnButtonSettingsSpecial4Clicked (object sender, System.EventArgs e)
		{
			CharactersDialog dlg = new CharactersDialog(m_ParentWindow);
			dlg.Character = m_SpecialChars.Char4;
			dlg.Run();
			if(dlg.Character != null)
			{
				m_SpecialChars.Char4 = dlg.Character;
				UpdateSpecialCharactersButtons();
			}
		}

		protected virtual void OnEntrySettingsPauseFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			try 
			{
				Double.Parse(entrySettingsPause.Text);
			}
			catch(FormatException)
			{
				MessageDialog md = new MessageDialog (m_ParentWindow, 
	                                      DialogFlags.DestroyWithParent,
				                          MessageType.Error, 
	                                      ButtonsType.Ok, Catalog.GetString("Incorrect pause time"));
				md.Run();
				md.Destroy();
				entrySettingsPause.Text = ""+0.8;
				entrySettingsPause.GrabFocus();
			}
		}

		protected virtual void OnButtonSettingsPlayWrongSoundClicked (object sender, System.EventArgs e)
		{
			MainWindow.PlaySoundFile(entrySettingsWrongSound.Text);
		}
		
		protected virtual void OnButtonSettingsPlayRightSoundClicked (object sender, System.EventArgs e)
		{
			MainWindow.PlaySoundFile(entrySettingsRightSound.Text);
		}

		protected virtual void OnCheckbuttonSettingsWrongClicked (object sender, System.EventArgs e)
		{
			entrySettingsWrongSound.Sensitive = checkbuttonSettingsWrong.Active;
			buttonSettingsOpenWrongSound.Sensitive = checkbuttonSettingsWrong.Active;
			buttonSettingsPlayWrongSound.Sensitive = checkbuttonSettingsWrong.Active;
		}

		protected virtual void OnCheckbuttonSettingsRightClicked (object sender, System.EventArgs e)
		{
			entrySettingsRightSound.Sensitive = checkbuttonSettingsRight.Active;
			buttonSettingsOpenRightSound.Sensitive = checkbuttonSettingsRight.Active;
			buttonSettingsPlayRightSound.Sensitive = checkbuttonSettingsRight.Active;
		}

		protected virtual void OnButtonSettingsOpenWrongSoundClicked (object sender, System.EventArgs e)
		{
			FileChooserDialog fc = CreateOpenSoundDialog(entrySettingsWrongSound.Text);
			
			if(fc.Run() == (int)ResponseType.Accept)
			{	
				entrySettingsWrongSound.Text = fc.Filename;
			}
			fc.Destroy();
		}

		protected virtual void OnButtonSettingsOpenRightSoundClicked (object sender, System.EventArgs e)
		{
			FileChooserDialog fc = CreateOpenSoundDialog(entrySettingsRightSound.Text);
			
			if(fc.Run() == (int)ResponseType.Accept)
			{	
				entrySettingsRightSound.Text = fc.Filename;
			}
			fc.Destroy();
		}

		public void PageSetupDialog()
		{
			PageSetupDialog dlg = new PageSetupDialog(m_ParentWindow, m_PageSettings, (string)m_document.Info["TITLE"]);
			if(dlg.Run() == (int)ResponseType.Ok)
			{
				m_document.Info["TITLE"] = dlg.PrintTitle;
			}
		}
			

		protected virtual void OnButtonSettingsPageClicked (object sender, System.EventArgs e)
		{
			PageSetupDialog();
		}

		protected virtual void OnEntrySettingsPractiseMaxNumFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			try 
			{
				Int32.Parse(entrySettingsPractiseMaxNum.Text);
			}
			catch(FormatException)
			{
				MessageDialog md = new MessageDialog (m_ParentWindow, 
	                                      DialogFlags.DestroyWithParent,
				                          MessageType.Error, 
	                                      ButtonsType.Ok, Catalog.GetString("Incorrect number"));
				md.Run();
				md.Destroy();
				entrySettingsPractiseMaxNum.Text = ""+10;
				entrySettingsPractiseMaxNum.GrabFocus();
			}
		}

	}
}
